using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Hematology.Data
{
	[DataObject]
	[Serializable]
	public partial class SiteUserSiteEvent
	{
		#region Constants

		private const string TableName = "[dbo].[SiteUser_SiteEvent]";

		#endregion
	
		#region Properties

		public int? UserId { get; set; }
		public int? EventId { get; set; }
		public DateTime? CreateDate { get; set; }
		public bool? IsDeleted { get; set; }

		#endregion
	
		#region Methods
	
		#region Mapping Methods
	
		protected void MapTo(DataSet ds)
		{
			if (ds == null)
				ds = new DataSet();

			if (ds.Tables[TableName] == null)
				ds.Tables.Add(TableName);
		
			ds.Tables[TableName].Columns.Add("UserId", typeof(Int32) );
			ds.Tables[TableName].Columns.Add("EventId", typeof(Int32) );
			ds.Tables[TableName].Columns.Add("CreateDate", typeof(DateTime) );
			ds.Tables[TableName].Columns.Add("IsDeleted", typeof(Boolean) );
		
			DataRow dr = ds.Tables[TableName].NewRow();

			dr["UserId"] = UserId ?? (object) DBNull.Value;
			dr["EventId"] = EventId ?? (object) DBNull.Value;
			dr["CreateDate"] = CreateDate ?? (object) DBNull.Value;
			dr["IsDeleted"] = IsDeleted ?? (object) DBNull.Value;

			ds.Tables[TableName].Rows.Add(dr);
		}
	
		protected void MapFrom(DataRow dr)
		{
			UserId = dr["UserId"] != DBNull.Value ? Convert.ToInt32(dr["UserId"]) : UserId = null;
			EventId = dr["EventId"] != DBNull.Value ? Convert.ToInt32(dr["EventId"]) : EventId = null;
			CreateDate = dr["CreateDate"] != DBNull.Value ? Convert.ToDateTime(dr["CreateDate"]) : CreateDate = null;
			IsDeleted = dr["IsDeleted"] != DBNull.Value ? Convert.ToBoolean(dr["IsDeleted"]) : IsDeleted = null;
		}
	
		public static List<SiteUserSiteEvent> MapFrom(DataSet ds)
		{
			// Initialise Collection.
			List<SiteUserSiteEvent> objects = new List<SiteUserSiteEvent>();
		
			// Validation.
			if (ds == null)
				throw new ApplicationException("Cannot map to dataset null.");
			
			if (ds.Tables[TableName].Rows.Count == 0)
				return objects;
		
			if (ds.Tables[TableName] == null)
				throw new ApplicationException("Cannot find table [dbo].[SiteUser_SiteEvent] in DataSet.");
		
			if (ds.Tables[TableName].Rows.Count < 1)
				throw new ApplicationException("Table [dbo].[SiteUser_SiteEvent] is empty.");
		
			// Map DataSet to Instance.
			foreach (DataRow dr in ds.Tables[TableName].Rows)
			{
				SiteUserSiteEvent instance = new SiteUserSiteEvent();
				instance.MapFrom(dr);
				objects.Add(instance);
			}
		
			// Return collection.
			return objects;
		}
	
		#endregion
	
		#region CRUD Methods
	
		public static SiteUserSiteEvent Get(Int32 userId, Int32 eventId)
		{
			SiteUserSiteEvent instance = new SiteUserSiteEvent();
		
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteUserSiteEvent_SELECT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, userId, eventId))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					if (ds == null || ds.Tables[0].Rows.Count == 0) throw new ApplicationException(String.Format("Could not get SiteUserSiteEvent ID:{0} {1} from Database.", userId, eventId));
					ds.Tables[0].TableName = TableName;
		
					instance.MapFrom( ds.Tables[0].Rows[0] );
				}
			}
			return instance;
		}
	
		#region INSERT

		private void Insert(Int32? userId, Int32? eventId, DateTime? createDate, Boolean? isDeleted, DbTransaction transaction = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteUserSiteEvent_INSERT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, userId, eventId, createDate, isDeleted))
			{
				if (transaction == null)
					db.ExecuteScalar(dbCommand);
				else
					db.ExecuteScalar(dbCommand, transaction);
			}
		}

		/// <summary>
		/// Insert current SiteUserSiteEvent to database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Insert(DbTransaction transaction = null)
		{
			Insert(UserId, EventId, CreateDate, IsDeleted, transaction);
		}

		#endregion
	
		#region UPDATE

		public static void Update(Int32? userId, Int32? eventId, DateTime? createDate, Boolean? isDeleted, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteUserSiteEvent_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@userId"].Value = userId;
				dbCommand.Parameters["@eventId"].Value = eventId;
				dbCommand.Parameters["@createDate"].Value = createDate;
				dbCommand.Parameters["@isDeleted"].Value = isDeleted;
		
				if (transaction == null)
					db.ExecuteNonQuery(dbCommand);
				else
					db.ExecuteNonQuery(dbCommand, transaction);
			}
		}
	
		public static void Update(Int32? userId, Int32? eventId, DateTime? createDate, Boolean? isDeleted)
		{
			Update(userId, eventId, createDate, isDeleted, null);
		}
	
		public static void Update(SiteUserSiteEvent siteUserSiteEvent)
		{
			siteUserSiteEvent.Update();
		}
	
		public static void Update(SiteUserSiteEvent siteUserSiteEvent, DbTransaction transaction)
		{
			siteUserSiteEvent.Update(transaction);
		}
	
		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		public void Update(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteUserSiteEvent_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@userId"].SourceColumn = "UserId";
				dbCommand.Parameters["@eventId"].SourceColumn = "EventId";
				dbCommand.Parameters["@createDate"].SourceColumn = "CreateDate";
				dbCommand.Parameters["@isDeleted"].SourceColumn = "IsDeleted";

				using (DataSet ds = new DataSet())
				{
					MapTo( ds );
					ds.AcceptChanges();
					ds.Tables[0].Rows[0].SetModified();

					if (transaction == null)
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, UpdateBehavior.Standard);
					else
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, transaction);
				}
			}
		}
	
		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		public void Update()
		{
			Update((DbTransaction)null);
		}
		#endregion
	
	
		#region DELETE

		public static void Delete(Int32? userId, Int32? eventId, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteUserSiteEvent_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, userId, eventId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
		}
	
		public static void Delete(Int32? userId, Int32? eventId)
		{
			Delete(
				userId, eventId);
		}
	
		/// <summary>
		/// Delete current SiteUserSiteEvent from database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		public void Delete(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteUserSiteEvent_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, UserId, EventId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
		}
	
		/// <summary>
		/// Delete current SiteUserSiteEvent from database.
		/// </summary>
		public void Delete()
		{
			Delete(null);
		}
	
		#endregion
	
		#region SEARCH

		private static List<SiteUserSiteEvent> Search(Int32? userId, Int32? eventId, DateTime? createDate, Boolean? isDeleted)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteUserSiteEvent_SEARCH";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, userId, eventId, createDate, isDeleted))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					ds.Tables[0].TableName = TableName;
					return MapFrom(ds);
				}
			}
		}

		private static List<SiteUserSiteEvent> Search(SiteUserSiteEvent searchObject)
		{
			return Search (searchObject.UserId, searchObject.EventId, searchObject.CreateDate, searchObject.IsDeleted);
		}
	
		#endregion
	
		#endregion
	
		#endregion
	}
}

